<?php

namespace app\home\controller;

use think\Controller;

class Upload extends Controller
{
	// 阿里云OSS的加速域名
	public $link = 'https://uploads.jiankangpao.sichuancancer.org/uploads';
	public $watermark = "-imageView";
	public $watermarkNo = "-imageView2";

	// https://jiankangpao.sichuancancer.org/azfzjkp_dev/home/Upload/test0362.html
	public function test0362()
	{
		// 图片地址 后面加时间戳是为了避免缓存
		// let img = 'https://uploads.jiankangpao.sichuancancer.org/uploads/images/20230203/c6fdf6abe193ee8d5e4e754571d9c27c.png-imageView2';
		// // 创建对象
		// let imgObj = new Image();
		// // 改变图片的src
		// imgObj.src = img;
		// imgObj.onload = function(){
		// // 打印
		// 	alert('width:'+imgObj.width+',height:'+imgObj.height);
		// };

		// list($width,$height) =  getimagesize ( "abc.png");
		list($width, $height) = getimagesize(ROOT_PATH . '..' . DS . 'uploads' . DS . 'images/20230203/c6fdf6abe193ee8d5e4e754571d9c27c.png');
		var_dump($width);
		var_dump($height);
	}

	// http://uploads.jiankangpao.sichuancancer.org/uploads/images/20230131/2ea1669f34f2f727f8b63e3266f81a4b.jpg?x-oss-process=style/imageView2
	// http://uploads.jiankangpao.sichuancancer.org/uploads/images/20230202/418b4730b68dc061d161d101055b68c6.png?x-oss-process=style/imageView2
	//表单上传
	public function uploadImage($name)
	{
		$files = request()->file($name);
		if (!isset($files)) {
			return 1;
		}

		$path_dest = '';
		foreach ($files as $file) {
			$info = $file->move(ROOT_PATH . '..' . DS . 'uploads' . DS . 'images');
			if ($info) {
				$path_dest .= ';' . $this->link . DS . 'images' . DS . $info->getSaveName() . $this->watermark;
			} else {
				return 2;
			}
		}
		$path_dest = substr($path_dest, strpos($path_dest, ';') + 1);
		return $path_dest;
	}

	//富文本上传
	public function kindeditorUpload($name = 'imgFile')
	{
		header("content-type:text/html;charset=utf-8");
		$file = request()->file($name);
		if (!isset($file)) {
			$res = array('error' => 1, 'message' => '文件没找到！');
		}
		$info = $file->validate(['ext' => 'jpg,png,gif,jpeg'])->move(ROOT_PATH . '..' . DS . 'uploads' . DS . 'images');
		if (empty($info)) {
			$res = array('error' => 1, 'message' => $file->getError());
			var_dump($res);
			return;
		}
		$res = array('error' => 0, 'url' => $this->link . DS . 'images' . DS . $info->getSaveName() . $this->watermark);
		header('Content-type: text/json');
		echo json_encode($res);
	}

	// uploadifive上传
	// https://jiankangpao.sichuancancer.org/azfzjkp_dev/home/Upload/upload.html
	public function upload($name = 'Filedata')
	{
		header("content-type:text/html;charset=utf-8");
		$file = request()->file($name);
		if (!isset($file)) {
			return server_response(1, '文件没找到！', $_FILES);
		}
		// echo ROOT_PATH . '..'.DS.'uploads'.DS.'images';
		// die();
		$fileType = input('fileType');
		if ($fileType == 'audio') {
			//音频：100 兆字节=104857600 字节
			$validate = ['size' => 104857600, 'ext' => 'mp3'];
		} else if ($fileType == 'video') {
			//视频：400 兆字节=419430400 字节
			$validate = ['size' => 419430400, 'ext' => 'mp4'];
		} else if ($fileType == 'other') {
			//400 兆字节=419430400 字节
			$validate = ['size' => 419430400];
		} else {
			$fileType = 'images';
			//图片限制：4 兆字节=4194304 字节
			$validate = ['size' => 4194304, 'ext' => 'jpg,png,gif,jpeg'];
		}

		$info = $file->validate($validate)->move(ROOT_PATH . '..' . DS . 'uploads' . DS . $fileType);
		if (empty($info)) {
			return server_response(2, "上传失败，文件格式错误或大小超过".($validate['size']/1024/1024)."M限制！", $_FILES);
		}

		if ($fileType == 'images') {
			$type = input('type');
			if ($type == 1) {
				//广告和新闻图片不加水印及投诉图片视频不加水印
				$res = $this->link . DS . $fileType . DS . $info->getSaveName() . $this->watermarkNo;
			} else {
				$res = $this->link . DS . $fileType . DS . $info->getSaveName() . $this->watermark;
			}
		} else {
			$res = $this->link . DS . $fileType . DS . $info->getSaveName();
		}

		// 尝试访问，让阿里云提前拉取
		http_2s($res);

		$rt = [];
		$rt['url'] = $res;

		if ($fileType == 'images') {
			// 图片取得宽高
			list($width, $height) = getimagesize(ROOT_PATH . '..' . DS . 'uploads' . DS . $fileType . DS . $info->getSaveName());
			$rt['width'] = $width;
			$rt['height'] = $height;
		} else {
			$rt['name'] = $_FILES['Filedata']['name'];
		}

		return server_response(0, '上传成功！！' . $_SERVER['SERVER_ADDR'], $rt);
	}

	//文件上传
	public function file_upload($name)
	{
		$file = request()->file($name);
		if (!isset($file)) {
			return 1;
		}

		$path_dest = '';
		$info = $file->move(ROOT_PATH . '..' . DS . 'uploads' . DS . 'files');
		if ($info) {
			$path_dest .= $this->link . DS . 'files' . DS . $info->getSaveName();
		} else {
			return 2;
		}
		return $path_dest;
	}

	//excel上传
	public function file_upload_result($name)
	{
		$file = request()->file($name);
		if (!isset($file)) {
			return 1;
		}

		$path_dest = '';
		$info = $file->validate(['ext' => 'xlsx,xls'])->move(ROOT_PATH . '..' . DS . 'uploads' . DS . 'files');
		if ($info) {
			$path_dest .= DS . 'home' . DS . 'wwwroot' . DS . 'default' . DS . 'uploads' . DS . 'files' . DS . $info->getSaveName();
		} else {
			return 2;
		}
		return $path_dest;
	}
}
